#!/bin/sh /etc/rc.common

START=19

MC=/tmp/macvlan_cleanup.sh

stop_macvlan() {
	local idx=0
	test -f $MC && {
		cat $MC | while read line; do
			echo "$idx#$line"
			idx=$((idx+1))
		done | sort -nr | while read line; do
			cmd=`echo "$line" | cut -d"#" -f2`
			$cmd
		done
		rm -f $MC
	}
}

add_macvlan() {
	local cfg="$1"
	local ifname index type mode flag
	local mvname

	config_get ifname "$cfg" ifname
	config_get index "$cfg" index
	config_get type "$cfg" type
	config_get mode "$cfg" mode
	config_get flag "$cfg" flag

	test -n "$ifname" || return 1
	test -n "$index" || return 1
	test -n "$type" || return 1

	mvname="${type}${index}${ifname}"

	echo "$ifname" | grep -q "^[a-zA-Z].*\.[0-9]*$" && {
		ifn=`echo $ifname | cut -d"." -f 1`
		iid=`echo $ifname | cut -d"." -f 2`
		test -n "$ifn" && ifconfig $ifn >/dev/null 2>&1 || return 1
		ifconfig $ifn up 2>/dev/null
		vconfig set_name_type DEV_PLUS_VID_NO_PAD
		vconfig add $ifn $iid 2>/dev/null && echo ip link del $ifname >>$MC
		mvname="${type}${index}${ifn}t${iid}"
	}

	ifconfig $ifname >/dev/null 2>&1 || return 1

	ifconfig $ifname up 2>/dev/null
	if [ "${type}" = "macvlan" ]; then
		ip link add link $ifname $mvname type ${type} 2>/dev/null && echo ip link del $mvname >>$MC
	elif [ "${type}" = "ipvlan" ]; then
		case "$mode" in
			"l2")
				ip link add link $ifname $mvname type ${type} mode ${mode} ${flag} 2>/dev/null && echo ip link del $mvname >>$MC
			;;
			"l3")
				ip link add link $ifname $mvname type ${type} 2>/dev/null && echo ip link del $mvname >>$MC
			;;
		esac
	fi
}

start() {
	stop_macvlan
	echo -n >$MC
	config_load macvlan
	config_foreach add_macvlan macvlan
}

stop() {
	stop_macvlan
}
